﻿/*
* 这里主要放的是使用regex获取字符串子串的方法实现
*/
#include "RegexHeaders.h"

int SpiltByRegex()
{
    string text = "机械臂品牌是aubo";
    regex pattern("(.*)品牌是(.*)");
    smatch results;
    if (regex_match(text, results, pattern)) {
        for (auto it = results.begin(); it != results.end(); ++it)
            cout << *it << endl;
    }
    else {
        cout << "match failed: " << text << endl;
    }

    string text2 = "DO4";
    regex pattern2("DO(.*)");
    smatch results2;
    if (!regex_match(text2, results2, pattern2))
    {
        cout << "match failed: " << text2 << endl;
    }
    auto a = results2[1];
    auto e = std::string(a);
    int num = atoi(e.c_str());
    cout << *(results2.begin() + 1) << endl;
    cout << num << endl;
    //cin.get();

    return 0;
}

int SpiltByRegex(string &in, regex pattern)
{
    try
    {
        string text = in;
        smatch results;
        if (regex_match(text, results, pattern)) {
            for (auto it = results.begin(); it != results.end(); ++it)
            {
                cout << *it << endl;
                //in = *it;
            }
        }
        else {
            cout << "match failed: " << text << endl;
        }
    }
    catch (const std::exception& err)
    {
        cout << err.what() << endl;
    }
    
    // 等待一个键盘输入
    //cin.get();

    return 0;
}

//using regex   = basic_regex<char>;
//using wregex  = basic_regex<wchar_t>;
//using cmatch  = match_results<const char*>;
//using wcmatch = match_results<const wchar_t*>;
//using smatch  = match_results<string::const_iterator>;
//using wsmatch = match_results<wstring::const_iterator>;
